﻿using JinJun.EF.Permission.BLL;
using JinJun.EF.Permission.Model;

using JinJun.EF.Permission.Web.Attributes;
using JinJun.EF.Permission.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace JinJun.EF.Permission.Web.Controllers
{
    public class HomeController : Controller
    {
        //主页
        [PermissionAttribute]
        public ActionResult Index()
        {
            return View();
        }



        //登录页面
        public ActionResult Login()
        {
            //取cookie里面的信息
            var cookie = System.Web.HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
            if (cookie != null)
            {
                //获取存在的cookie
                var ticket = FormsAuthentication.Decrypt(cookie.Value);
                string userData = ticket.UserData;
                if (!string.IsNullOrEmpty(userData))
                {
                    //存储在session里面
                    AdminUser adminUser= new AdminUser();
                    adminUser.Id = Convert.ToInt32(userData.Split('#')[0]);
                    adminUser.Name = userData.Split('#')[1];
                    adminUser.Password = userData.Split('#')[2];
                    AdminUserContext.adminContext.AdminUser =adminUser;
                    return Redirect("/Home/Index");
                }
            }
            return View();
        }


        //管理员列表页面
        [PermissionAttribute]
        public ActionResult Administrator()
        {
            return View();
        }


        //权限页面
        [PermissionAttribute]
        public ActionResult AdminCompetence()
        {
            return View();
        }


        //添加权限页面
        [PermissionAttribute]
        public ActionResult Competence()
        {
            //创建菜单业务层对象
            MenuService roleService = new MenuService();
            //定义查询父类菜单集合拉姆达表达式
            Expression<Func<Menu, bool>> parentLambda = a => a.ParentId == 0;
            //接收父类List集合
            ViewBag.parentList = roleService.QueryList(parentLambda);
            //定义查询子类菜单集合拉姆达表达式
            Expression<Func<Menu, bool>> sonLambda = a => a.ParentId != 0;
            //接收子类List集合
            ViewBag.sonList = roleService.QueryList(sonLambda);
            //定义查询功能集合拉姆达表达式
            Expression<Func<Function, bool>> functionLambda = a=>a.MenuId!=0;
            //创建功能业务层对象
            FunctionService functionService = new FunctionService();
            //接收功能List集合
            ViewBag.functionList =functionService.QueryList(functionLambda);
            //创建组业务层对象
            ModuleService moduleService = new ModuleService();
            //定义查询组集合的拉姆达表达式
            Expression<Func<Module, bool>> moduleLambda = a => a.Id != 0;
            //接收组列表集合
            ViewBag.moduleList = moduleService.QueryList(moduleLambda);
            return View();
        }



        //修改权限页面
        [PermissionAttribute]
        public ActionResult UpdateCompetence(int id)
        {
            //定义角色菜单功能关联表业务层对象
            BaseService<RoleFunctionPermission> service = new BaseService<RoleFunctionPermission>();
            //定义查询角色菜单功能关联表集合拉姆达表达式
            Expression<Func<RoleFunctionPermission, bool>> roleFunctionLambda = a => a.RoleId == id;
            //接收集合
            ViewBag.roleFunctionList = service.QueryList(roleFunctionLambda);
            //创建角色业务层对象
            RoleService roleService = new RoleService();
            //定义查询角色表对象拉姆达表达式
            Expression<Func<Role, bool>> roleLambda = a => a.Id == id;
            //接收对象
            ViewBag.role = roleService.Query(roleLambda);
            //创建角色和组关联业务层
            BaseService<ModuleRole> baseService = new BaseService<ModuleRole>();
            //定义查询角色和组关联表对象拉姆达表达式
            Expression<Func<ModuleRole, bool>> moduleRoleLambda = a => a.RoleId == id;
            //接收对象
            ViewBag.moduleRole =baseService.Query(moduleRoleLambda);
            //创建菜单业务层对象
            MenuService menuService = new MenuService();
            //定义查询父类菜单集合拉姆达表达式
            Expression<Func<Menu, bool>> parentLambda = a => a.ParentId == 0;
            //接收父类List集合
            ViewBag.parentList = menuService.QueryList(parentLambda);
            //定义查询子类菜单集合拉姆达表达式
            Expression<Func<Menu, bool>> sonLambda = a => a.ParentId != 0;
            //接收子类List集合
            ViewBag.sonList = menuService.QueryList(sonLambda);
            //定义查询功能集合拉姆达表达式
            Expression<Func<Function, bool>> functionLambda = a => a.MenuId != 0;
            //创建功能业务层对象
            FunctionService functionService = new FunctionService();
            //接收功能List集合
            ViewBag.functionList = functionService.QueryList(functionLambda);
            //创建组业务层对象
            ModuleService moduleService = new ModuleService();
            //定义查询组集合的拉姆达表达式
            Expression<Func<Module, bool>> moduleLambda = a => a.Id != 0;
            //接收组列表集合
            ViewBag.moduleList = moduleService.QueryList(moduleLambda);
            return View();
        }

        //组列表页面
        [PermissionAttribute]
        public ActionResult GroupList()
        {
            return View();
        }


        //系统日志页面
        [PermissionAttribute]
        public ActionResult SystemLogs()
        {
            return View();
        }

        //菜单管理页面
        [PermissionAttribute]
        public ActionResult MenuList()
        {
            return View();
        }

        //功能管理页面
        [PermissionAttribute]
        public ActionResult FunctionList()
        {
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
        public ActionResult AddAdmin()
        {          
            return View();
        }
    }
}